<?php
//session_start();
session_start();
require_once("database.php");
$conn=mysql_link();
//$name=$_SESSION['username'];
//echo $name;
//$sql="SELECT * FROM borrowrecords where re=1 and UserID=1";
//$sql1= mysqli_query($conn,$sql);
//$res=mysqli_fetch_all($sql1,MYSQLI_ASSOC);

$us=$_SESSION['email'];
$cha="SELECT Name FROM users where Email='$us'";
$sql2= mysqli_query($conn,$cha);
$res2= mysqli_fetch_assoc($sql2);
$username = $res2["Name"];

$sql="SELECT borrowrecords.*,users.* FROM borrowrecords INNER JOIN users on borrowrecords.UserID = users.UserID  where re=1 and Name='$username'";
$sql1= mysqli_query($conn,$sql);
$res=mysqli_fetch_all($sql1,MYSQLI_ASSOC);
//print_r($res);
//echo($username);
if ($_GET["action"]=="jieyue"){
//    print_r($res);
    $Number=$_POST['Number'];
    $bookName=$_POST['bookName'];
    $user=$_POST['user'];
    if (empty($Number and $bookName and $user)){
        echo "<script> alert('请填写完整信息')</script>";
    }else{
        $mysql="SELECT * FROM borrowrecords where UserID='$user'";
        $mysql="SELECT borrowrecords.*,users.* FROM borrowrecords INNER JOIN users on borrowrecords.UserID = users.UserID where Name='$username'";
        $sql1= mysqli_query($conn,$mysql);
        $res1= $sql1->num_rows;
        if ($res1 > 0){
            $UPDATE="UPDATE borrowrecords SET re = 2 WHERE Bookname='$bookName';";
            $UPDATE1= mysqli_query($conn,$UPDATE);
            echo "<script> alert('成功')</script>";
        }else{
            echo "<script> alert('请填写正确信息')</script>";
            echo "<script> location.href = 'return.php'</script>";
            die();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/return.css">
    <link href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css" rel="stylesheet" />
</head>

<body>
    <!--背景-->

        <!-- 左手边菜单栏 -->
        <div class="all">
            <!-- 左手边菜单栏 -->
            <div class="h left_Menu_Bar Center">
                <div class="Menu_Bar Center">
                    <!-- 头像 -->
                    <div class="avatar">
                        <!-- 默认头像 -->
                        <h1>读</h1>
                    </div>
                    <!-- 菜单 -->
                    <div class="Menu Font">
                        <ul class="Center ct">
                            <li><a href="#">用户管理</a></li>
                            <li>
                                <a href="#">借阅管理</a> <button class="btn" id="btn"><i class="bx bx-chevron-down"
                                        id="arrow"></i></button>
                            </li>
                            <!-- 下拉菜单 -->
                            <div class="box " id="dropdown">
                                <ul class="drop_down_menu Center">
                                    <li><a href="../html/borrow.html">借阅</a></li>
                                    <li><a href="#">归还</a></li>
                                    <li><a href="#">记录</a></li>
                                    <li><a href="#">过期</a></li>
                                </ul>
                            </div>

                            <li><a href="#">图书列表</a></li>

                        </ul>
                    </div>
                </div>
            </div>

        <!-- <div class="h bodyed"> -->
        <!-- 白色背景 -->
            <div class="Background">
                <!-- 标题 -->
                <h1 class="title">图书归还</h1>
                <form action="?action=jieyue" id="form" method="post">
                    <div class="banner">
                        <div class="up_inp">
                            <div><b>条形码</b><input name="Number" type="text" class="Number"></div>
                            <div><b>书籍</b><input name="bookName" type="text" class="bookName"></div>
                            <div><b>用户名</b><input name="user" type="text" class="bookName"></div>
                        </div>


                        <div class="yes_no">
                            <button class="add" name="button">确认</button>
                        </div>

                        <div class="content">
                            <table>
                                <thead>
                                    <tr>
                                        <th>索书号</th>
                                        <th>条形码</th>
                                        <th>书籍</th>
                                        <th>文献所属地</th>
                                        <th>借阅时间</th>
                                        <th>应还时间</th>
                                    </tr>
                                </thead>
                                <tbody></tbody>

                                <!-- ------------内容-------------- -->
                                <?foreach ($res as $value){?>
                                <tr>
                                        <th><?echo $value['RecordID']?></th>
                                        <th><?echo $value['BookID']?></th>
                                        <th><?echo $value['Bookname']?></th>
                                        <th><?echo $value['di']?></th>
                                        <th><?echo $value['BorrowDate']?></th>
                                        <th><?echo $value['ReturnDate']?></th>
                                    </tr>
                                <?}?>
                                <!-- ------------内容------------- -->

                            </table>
                        </div>
                    </div>
                    <h6>没有更多了~</h6>
                </div>
            </form>
            <div class="not">
                <input type="button" id="clearInput" value="取消"></input >
                <script>
                    document.getElementById('clearInput').addEventListener('click',function(){
                        let myInputs = document.querySelectorAll('input[type="text"]')
                        myInputs.forEach(function(input){
                            input.value = ''
                        });
                    })
                </script>
            </div>
        </div>
</body>

</html>
<script src="../js/return.js"></script>